package com.ujiuye.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.ujiuye.config.*;
import com.ujiuye.pojo.Setmeal;
import com.ujiuye.service.ISetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中间表  一个套餐有什么检查组 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private ISetmealService setmealService;
    @Resource
    private RedisTemplate<String,String> template;

    @GetMapping("SetMealCount")
    public Result listSetMealCount(){
        try {
            List<Map<String, Object>> maps = setmealService.listSetMealCount();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,maps);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }


    @PostMapping("list")
    public PageResult list(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult page = setmealService.listPage(queryPageBean);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    @GetMapping
    public Result listAll(){
        try {
            List<Setmeal> list = setmealService.list();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @Value("${upload}")
    private String path;
    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile){
        File file = MyFileUtils.upload(multipartFile, path);
        if (file!=null){
            //获取Redisset集合
            SetOperations<String, String> opsForSet = template.opsForSet();
            //当图片上传成功后将图片名字存入Redis中
            opsForSet.add("SETMEAL_PIC_UPLOAD",file.getName());
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,file.getName());
        }
        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
    }


    @PostMapping("save")
    public Result save(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
        try {
            setmealService.save(checkgroupIds,setmeal);
            //获取Redisset集合
            SetOperations<String, String> opsForSet = template.opsForSet();
            //取出存在数据库的名称进行截取 从字符串最后开始截取截取到/后面的字符串
            String img = setmeal.getImg().substring(setmeal.getImg().lastIndexOf("/") + 1);
            //当图片保存成功后将图片名字存入Redis中
            opsForSet.add("SETMEAL_PIC_DB",img);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    @DeleteMapping("/delete/{id}")
    public Result remove(@PathVariable("id") int id){
        try {
            setmealService.removeById(id);
            return new Result(true,"删除套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(true,"删除套餐失败");
        }
    }
    @GetMapping("/find/{id}")
   public Result getById(@PathVariable("id") int id){
        try {
            Result result = setmealService.getById(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }

   }

   @PostMapping("/update")
   public Result update(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
       try {
           setmealService.update(checkgroupIds,setmeal);
           return new Result(true,"套餐编辑成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"套餐编辑失败");
       }
   }
   @RequestMapping("download")
   public ResponseEntity<byte[]> download() throws IOException {
        //查询数据
       List<Setmeal> list = setmealService.list();
       //将查询数据写入excel中
       File file = listToExcel(list);
       //下载
       return DownLoad.download(file);
   }


   private File listToExcel(List<Setmeal> list){
        //创建工作簿
       HSSFWorkbook workbook = new HSSFWorkbook();
       //创建工作表
       HSSFSheet sheet = workbook.createSheet("套餐信息");
       //创建第1行
       HSSFRow row = sheet.createRow(0);
       //第一行展示的数据
       row.createCell(0).setCellValue("id");
       row.createCell(1).setCellValue("名字");
       row.createCell(2).setCellValue("编码");
       row.createCell(3).setCellValue("助记码");
       row.createCell(4).setCellValue("性别");
       row.createCell(5).setCellValue("年龄");
       row.createCell(6).setCellValue("价格");
       row.createCell(7).setCellValue("描述");
       for (int i = 0; i < list.size(); i++) {
           Setmeal setmeal = list.get(i);
           HSSFRow row1 = sheet.createRow(i + 1);
           row1.createCell(0).setCellValue(setmeal.getId());
           row1.createCell(1).setCellValue(setmeal.getName());
           row1.createCell(2).setCellValue(setmeal.getCode());
           row1.createCell(3).setCellValue(setmeal.getHelpcode());
           String sex = "不限";
           if (setmeal.getSex().equals("1")){
               sex = "男";
           }
           if (setmeal.getSex().equals("2")){
               sex = "女";
           }
           row1.createCell(4).setCellValue(sex);
           row1.createCell(5).setCellValue(setmeal.getAge());
           row1.createCell(6).setCellValue(setmeal.getPrice());
           row1.createCell(7).setCellValue(setmeal.getRemark());
       }
       File file = new File(path+"套餐信息.xls");
       try {
           workbook.write(file);
       } catch (IOException e) {
           e.printStackTrace();
       }
       return file;
   }






}

